package redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class RedisDemoSet {
    public static void test1(Jedis jedis) {
        System.out.println("sadd  smembers");
        jedis.flushAll();

        jedis.sadd("key","111","222","333");
        Set<String> value =  jedis.smembers("key");
        System.out.println("value：" + value);
    }
    private static void test2(Jedis jedis) {
        System.out.println("sismember 查看key中是否有所求元素");
        jedis.flushAll();

        jedis.sadd("key","111","222","333");
        boolean rest = jedis.sismember("key","444");
        System.out.println("no 444:"+rest);
        rest = jedis.sismember("key","222");
        System.out.println("has 222:"+rest);
    }
    public static void test3(Jedis jedis){
        System.out.println("scard 查看set中的元素个数");
        jedis.flushAll();

        jedis.sadd("key","111","222","333");
        long rest = jedis.scard("key");
        System.out.println(rest);
    }

    public static void test4(Jedis jedis){
        System.out.println("spop");
        jedis.flushAll();

        jedis.sadd("key","111","222","333","555","6666");
        String value = jedis.spop("key");
        System.out.println("value:"+value);
        value = jedis.spop("key");
        System.out.println("value:"+value);
        value = jedis.spop("key");
        System.out.println("value:"+value);
    }
    public static void test5(Jedis jedis) {
        System.out.println("sinter 两个集合求交集");
        jedis.flushAll();

        jedis.sadd("key1","2222","333","555","1111");
        jedis.sadd("key2","2222","777","666","555","333");
        Set<String> rest = jedis.sinter("key1","key2");
        System.out.println(rest);
    }
    public static void test6(Jedis jedis){
        System.out.println("sinterstore 两个集合求交集后的结果用key3接受");
        jedis.flushAll();

        jedis.sadd("key1","2222","333","555","1111");
        jedis.sadd("key2","2222","777","666","555","");
        long ret = jedis.sinterstore("key3","key1","key2");
        System.out.println(ret);
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()) {
            //test1(jedis);
            //test2(jedis);
            //test3(jedis);
            //test4(jedis);
            //test5(jedis);
            test6(jedis);
        }
    }


}
